<head>
<?php $this->load->view('head'); ?>
</head>
<div id="wrapper">
	<div id="content-container">
        <div id="content-main">
	       <div class="content-auto">
            <?php
            if(isset($reports) && is_array($reports) && count($reports) ) {
            $i=1;
            ?>
            <table>
                 <thead>
                        <tr>
                            <td style="border:0" align="left" colspan="2">
                                    <span class='textunderline_blue_titlerpt'>CHAMROEUN&nbsp;Microfinance&nbsp;Limited
                                    <br />
                                    </span><br /><br />
                                    <strong><?php echo $title; ?></strong><br />
                                    <strong>Office: <?php echo $xlbranch;?></strong><br />
                                    <strong>
                                    <?php 
                                    $fields1 = explode('-',$datestart);
                                    $fields2 = explode('-',$dateend);
                                    $date1 = $fields1[1].'-'.$fields1[0].'-1';
                                    $date2 = $fields2[1].'-'.$fields2[0].'-1';
                                    if($fields1[0]==$fields2[0]){
                                        echo date("M-Y", strtotime($date1));    
                                    }else{
                                        echo date("M-Y", strtotime($date1)).'&nbsp;&nbsp;to&nbsp;&nbsp;'.date("M-Y", strtotime($date2));
                                    }
                                    ?>
                                    </strong>
                            </td>
                       </tr>
                </thead>
            </table>
            <table class="table table-bordered table-striped" cellspacing="0" cellpadding="0" border="1">
            	  <thead>
            	    <tr>
                        <th rowspan="2"><strong>No</strong></th>
                        <th rowspan="2"><strong>Description</strong></th>
                        <th colspan="2"><strong>Addition(+)</strong></th>
                        <th colspan="2"><strong>Used(-)</strong></th>
                        <th colspan="2"><strong>Broken(-)</strong></th>
                        <th colspan="2"><strong> Ending Balance</strong></th>
                        <th rowspan="2"><strong>QTY<br /> Counted</strong></th>
                        <th rowspan="2"><strong>Differrent</strong></th>
            	    </tr>
                    <tr>
                        <td><strong>QTY</strong></td>
                        <td><strong>Amount</strong></td>
                        <td><strong>QTY</strong></td>
                        <td><strong>Amount</strong></td>
                        <td><strong>QTY</strong></td>
                        <td><strong>Amount</strong></td>
                        <td><strong>QTY</strong></td>
                        <td><strong>Amount</strong></td>
                    </tr>
            	  </thead>
            	  <tbody>
              	<?php 
                    $buy = 0;
                    $amtbuy = 0;
                    $used = 0;
                    $amtused = 0;
                    $broken = 0; 
                    $amtbroken = 0;$stockin = 0;$amtstock = 0;$sumtotal=0;
            		foreach ($reports as $item){
            			echo "<tr class='first' valign='top'>\n";
                        echo "<td align='center'>".$i++."</td>\n";
                        echo "<td align='center'>".$item->item."</td>\n";
                        echo "<td align='center'>".number_format($item->buy)."</td>\n";
                        echo "<td align='center'>".number_format($item->totalbuy)."</td>\n";
                        echo "<td align='center'>";
                            echo !empty($item->unitused) ? number_format($item->unitused) : 0;
                        echo "</td>\n";
                        echo "<td align='center'>";
                            echo !empty($item->totalused) ? number_format($item->totalused) : 0;
                        echo "</td>\n";
                        echo "<td align='center'>";
                            echo !empty($item->unitbroken) ? number_format($item->unitbroken) : 0;
                        echo "</td>\n";
                        echo "<td align='center'>";
                            echo !empty($item->totalbroken) ? number_format($item->totalbroken) : 0;
                        echo "</td>\n";
                        echo "<td align='center'>".number_format($item->balance)."</td>\n";
                        echo "<td align='center'>".number_format($item->amount)."</td>\n";
                    if($cat==1 || !empty($type)){
                        echo "<td align='center'>-</td>\n";
                        echo "<td align='center'>-</td>\n";
                    }else{
                        echo "<td align='center'>";
                            $utotal = !empty($item->total) ? $item->total : '0';
                            $sumtotal += $utotal;
                            echo number_format($utotal);
                        echo "</td>\n";
                        echo "<td align='center'>\n";
                            echo number_format($utotal - $item->balance);
                        echo "</td>";
                    }
                        echo "</tr>\n";
                        
                        $buy += $item->buy;
                        $amtbuy += $item->totalbuy;
                        $used += $item->unitused;
                        $amtused += $item->totalused;
                        $broken += $item->unitbroken;
                        $amtbroken += $item->totalbroken;
                        $stockin += $item->balance;
                        $amtstock += $item->amount;
            		}
                    echo "<tr>\n";
                    echo "<td colspan='2' style='text-align: right;'><strong>Total</strong></td>\n";
                    echo "<td align='center'><strong>".number_format($buy)."</strong></td>\n";
                    echo "<td align='center'><strong>".number_format($amtbuy)."</strong></td>\n";
                    echo "<td align='center'><strong>".number_format($used)."</strong></td>\n";
                    echo "<td align='center'><strong>".number_format($amtused)."</strong></td>\n";
                    echo "<td align='center'><strong>".number_format($broken)."</strong></td>\n";
                    echo "<td align='center'><strong>".number_format($amtbroken)."</strong></td>\n";
                    echo "<td align='center'><strong>".number_format($stockin)."</strong></td>\n";
                    echo "<td align='center'><strong>".number_format($amtstock)."</strong></td>\n";
                if($cat==1 || !empty($type)){
                    echo "<td align='center'><strong>".number_format(count($totalcount) ? $totalcount['total'] : 0 )."</strong></td>\n";
                    echo "<td align='center'><strong>".number_format(count($totalcount) ? $totalcount['total']-$stockin : 0 )."</strong></td>\n";
                }else{
                    echo "<td align='center'><strong>".$sumtotal."</strong></td>\n";
                    echo "<td align='center'><strong>".($sumtotal - $stockin)."</strong></td>\n";
                    echo "</tr>\n";
                }
              	?>
              </tbody>
            </table>
            <?php }?>
            </div>
	   </div>
	</div>
</div>
<?php 
    $filename=$xlname.'_'.date('Ymdhis', time()).".xls";
	header('Content-type: application/vnd.ms-excel');
    header("Content-Disposition: attachment; filename=$filename");
    header("Pragma: no-cache");
    header("Expires: 0");
?>
